<?php

namespace App\Helpers;

class ClientHelper
{

    /**
     * 获取真实IP地址
     * @return string
     */
    public static function getIp()
    {
        if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknow")){
            $ip = getenv("HTTP_CLIENT_IP");
        }else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknow")){
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknow")){
            $ip = getenv("REMOTE_ADDR");
        }else if(isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"],"unknow")){
            $ip = $_SERVER["REMOTE_ADDR"];
        }else{
            $ip = "unknow";
        }
        return $ip;
    }

    /**
     * 获取浏览器类型
     * @return string
     */
    public static function getBrowser()
    {
        $agent = $_SERVER["HTTP_USER_AGENT"];

        if(empty($agent)) {
            return 'unknow';
        }else if(strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) {
            return "ie";
        }else if(strpos($agent, 'Firefox') !== false) {
            return "firefox";
        }else if(strpos($agent, 'Chrome') !== false) {
            return "chrome";
        }else if(strpos($agent, 'Opera') !== false) {
            return 'opera';
        }else if((strpos($agent, 'Chrome') == false) && strpos($agent, 'Safari') !== false) {
            return 'safari';
        }else {
            return 'unknown';
        }
    }

    /**
     * 获取浏览器版本
     * @return string
     */
    public static function getBrowserVersion()
    {
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if(empty($_SERVER['HTTP_USER_AGENT'])) {
            return 'unknow';
        }else if(preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        }else if(preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        }else if(preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        }else if(preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        }else if((strpos($agent,'Chrome') == false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs)) {
            return $regs[1];
        }else if(preg_match('/rv:(\d+)\..*/i', $agent, $regs)) {
            return $regs[1];
        }else {
            return 'unknow';
        }
    }

    /**
     * 获取操作系统
     * @return string
     */
    public static function getOs()
    {
        if(empty($_SERVER['HTTP_USER_AGENT'])) return 'unknow';

        $osList = array(
            '/windows nt 10/i'      =>  'Windows 10',
            '/windows nt 6.3/i'     =>  'Windows 8.1',
            '/windows nt 6.2/i'     =>  'Windows 8',
            '/windows nt 6.1/i'     =>  'Windows 7',
            '/windows nt 6.0/i'     =>  'Windows Vista',
            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
            '/windows nt 5.1/i'     =>  'Windows XP',
            '/windows xp/i'         =>  'Windows XP',
            '/windows nt 5.0/i'     =>  'Windows 2000',
            '/windows me/i'         =>  'Windows ME',
            '/win98/i'              =>  'Windows 98',
            '/win95/i'              =>  'Windows 95',
            '/win16/i'              =>  'Windows 3.11',
            '/macintosh|mac os x/i' =>  'Mac OS X',
            '/mac_powerpc/i'        =>  'Mac OS 9',
            '/linux/i'              =>  'Linux',
            '/ubuntu/i'             =>  'Ubuntu',
            '/iphone/i'             =>  'iPhone',
            '/ipod/i'               =>  'iPod',
            '/ipad/i'               =>  'iPad',
            '/android/i'            =>  'Android',
            '/blackberry/i'         =>  'BlackBerry',
            '/webos/i'              =>  'Mobile'
        );

        foreach ($osList as $regex => $value)
        {
            if(preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) return $value;
        }

        return 'unknown';
    }


    /**
     * 判断是否是utf8编码
     * @param $string
     * @return bool
     */
    public static function isUTF8($string)
    {
        $len  = strlen($string);
        for($i=0; $i<$len; $i++)
        {
            $c = ord($string[$i]);
            if($c > 128)
            {
                if(($c >= 254)) return false;
                elseif($c >= 252) $bits=6;
                elseif($c >= 248) $bits=5;
                elseif($c >= 240) $bits=4;
                elseif($c >= 224) $bits=3;
                elseif($c >= 192) $bits=2;
                else return false;
                if(($i+$bits) > $len) return false;
                while($bits > 1)
                {
                    $i++;
                    $b=ord($string[$i]);
                    if($b < 128 || $b > 191) return false;
                    $bits--;
                }
            }
        }
        return true;
    }

}
